package com.nowcoder.code.lcof;

import com.algorithm.model.TreeNode;

public class JZ86 {

    boolean find = false;

    public int lowestCommonAncestor(TreeNode root, int o1, int o2) {
        if (root == null || find) {
            return -1;
        }
        int left = lowestCommonAncestor(root.left, o1, o2);
        int right = lowestCommonAncestor(root.right, o1, o2);
        if (left != -1 && right != -1) {
            find = true;
            return root.val;
        }
        if (root.val == o1 || root.val == o2) {
            if (left != -1 || right != -1) {
                find = true;
            }
            return root.val;
        }
        return Integer.max(left, right);
    }

}
